package phapp.service.custom.impl;

import sun.misc.*;

import java.io.File;
import java.sql.Timestamp;
import java.util.*;

import net.sf.json.*;

import org.apache.poi.hssf.record.formula.functions.Rand;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.*;
import phapp.model.data.Constellation;
import phapp.model.data.Curve;
import phapp.model.data.Qfdtext;
import phapp.model.luckstar.Stardeal;
import phapp.model.luckstar.Staruse;
import phapp.model.qfd.Custqfd;
import phapp.model.system.Menu;
import phapp.model.system.Version;
import phapp.dao.custom.*;
import phapp.service.common.CommonUtils;
import phapp.service.common.QfdException;
import phapp.service.common.SendTextMail;
import phapp.service.custom.impl.*;
import phapp.service.custom.*;
import phapp.service.data.ConstellationManager;
import phapp.service.data.CurveManager;
import phapp.service.data.QfdtextManager;
import phapp.service.luckstar.StardealManager;
import phapp.service.luckstar.StaruseManager;
import phapp.service.system.VersionManager;

/**
 * Title: t_custom_info 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("customManager")
@Transactional
public class CustomManagerImpl extends BaseManager<Custom, java.lang.Integer> implements CustomManager
{
   public static final String CUSTOM_IMG_URL_PREFIX = "http://117.40.178.58:9090/qfd/upload/customimg";
   public static final String CUSTOM_IMG_FILE_PATH = "/app/qfdapp/webapps/qfd/upload/customimg";
   //public static final String CUSTOM_IMG_URL_PREFIX = "/app/qfdapp/webapps/qfd/upload/customimg";
   //public static final String CUSTOM_IMG_FILE_PATH = "F:/Code/Project/QFD/WebContent/upload/customimg";
 
   public static final int CUSTOM_PRAYLUCK_STAR = 1;

   
   private CustomDao customDao;
   private ConstellationManager constellationManager;
   private StardealManager stardealManager;
   private StaruseManager staruseManager;
   private VersionManager versionManager;
   private QfdtextManager qfdtextManager;
   private CurveManager curveManager;

   public void setCustomDao(CustomDao dao)
   {
      this.customDao = dao;
   }
   
   public void setConstellationManager(ConstellationManager constellationManager)
   {
      this.constellationManager = constellationManager;
   }

   public void setStardealManager(StardealManager stardealManager)
   {
      this.stardealManager = stardealManager;
   }

   public void setStaruseManager(StaruseManager staruseManager)
   {
      this.staruseManager = staruseManager;
   }

   public void setVersionManager(VersionManager versionManager)
   {
      this.versionManager = versionManager;
   }

   public void setQfdtextManager(QfdtextManager qfdtextManager)
   {
      this.qfdtextManager = qfdtextManager;
   }

   public void setCurveManager(CurveManager curveManager)
   {
      this.curveManager = curveManager;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.customDao;
   }

   /**
    * <p>
    * 分页查询Custom<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return customDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Custom<br>
    * 
    * @param Custom
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = true)
   public Boolean removeByIdArry(Custom u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = customDao.removeByIdArry(u);
      if (i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }
   
   public Map customRegistInfo(JSONObject jsonData)
   {
      List listData = null;
      byte[] btFile = null;
      String CustAccount = "";
      Custom customData = null;
      Random randNumber = null;
      int nStatus = 0, nValue = 0;
      List<Custom> listQuery = null;
      String strFileType = "", strFileName = "", strFileData = "", strFileUrl = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() > 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2001);

         randNumber = new Random();
         mapQuery.clear();
         mapQuery.put("dataDay", 1);
         listData = curveManager.findList(mapQuery);
         nValue = java.lang.Math.abs(randNumber.nextInt());
         nValue = nValue%listData.size();
         
         strFileType = jsonData.getString("filetype");
         strFileData = jsonData.getString("filedata");
         btFile = decodeBASE64(strFileData);
         
         strFileName = CommonUtils.formatDateTime() + CommonUtils.generateSeq() + "." + strFileType;
         strFileUrl = CUSTOM_IMG_URL_PREFIX + "/" + strFileName;
         strFileName = CUSTOM_IMG_FILE_PATH + File.separator + strFileName;
         CommonUtils.saveFileData(strFileName, btFile);

         customData = new Custom();
         customData.setCustAccount(jsonData.getString("user"));
         customData.setCustPasswd(jsonData.getString("pass"));
         customData.setCustEmail(jsonData.getString("email"));
         customData.setCustImgfile(strFileName);
         customData.setCustImgurl(strFileUrl);

         if(jsonData.getString("name") != null && jsonData.getString("name").length() > 0)
            customData.setCustName(jsonData.getString("name"));
         if(jsonData.getString("city") != null && jsonData.getString("city").length() > 0)
            customData.setCustCity(jsonData.getString("city"));
         if(jsonData.getString("birthday") != null && jsonData.getString("birthday").length() > 0)
            customData.setCustBirthday(jsonData.getString("birthday"));
         if(jsonData.getString("sex") != null && jsonData.getString("sex").length() > 0)
            customData.setCustSex(jsonData.getInt("sex"));
         
         customData.setCurveType(nValue);
         customData.setCustStatus(1);
         customData.setLoginCount(0);
         customData.setTotalStar(0);
         customData.setPraiseCount(0);
         customData.setWishCount(0);
         customData.setOtherwishCount(0);
         customData.setAttentionCount(0);
         
         save(customData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eRegist)
      {
         nStatus = 10000;
         eRegist.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   public Map customRegist(JSONObject jsonData)
   {
      int nStatus = 0, nValue = 0;
      String CustAccount = "";
      Custom customData = null;
      Random randNumber = null;
      List listData = null;
      List<Custom> listQuery = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() > 0 || CustAccount == null || CustAccount.length() == 0)
         {
            nStatus = 2001;
         }
         else
         {
            randNumber = new Random();
            mapQuery.clear();
            mapQuery.put("dataDay", 1);
            listData = curveManager.findList(mapQuery);
            nValue = java.lang.Math.abs(randNumber.nextInt());
            nValue = nValue%listData.size();
            
            customData = new Custom();
            customData.setCustAccount(jsonData.getString("user"));
            customData.setCustPasswd(jsonData.getString("pass"));
            customData.setCustEmail(jsonData.getString("email"));
            customData.setCustBirthday("19800101");
            customData.setCustSex(0);
            customData.setCurveType(nValue);
            customData.setLoginCount(0);
            customData.setCustStatus(1);
            
            customData.setTotalStar(0);
            customData.setPraiseCount(0);
            customData.setWishCount(0);
            customData.setOtherwishCount(0);
            customData.setAttentionCount(0);
            
//            customData.setCustName(jsonData.getString("name"));
//            customData.setCustMobile(jsonData.getString("mobile"));
//            customData.setCustEmail(jsonData.getString("email"));
//            customData.setCustSex(jsonData.getString("sex")!=null ? new Integer(jsonData.getString("sex")) : 0);
//            customData.setCustBirthday(jsonData.getString("birthday"));
//            customData.setCustVocation(jsonData.getString("vocation"));
            
            save(customData);
            nStatus = 1000;
         }
      
//      starData = new 
//      stardealManager.save(starData);
      }
      catch(Exception eRegist)
      {
         nStatus = 10000;
         eRegist.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "注册成功！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else
            mapResult.put("message", "注册失败！");
      }
      
      return mapResult;
   }
   
   public Map customSupply(JSONObject jsonData)
   {
      int nStatus = 0;
      byte[] btFile = null;
      String CustAccount = "";
      Custom customData = null;
      List<Custom> listQuery = null;
      String strFileType = "", strFileName = "", strFileData = "", strFileUrl = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         strFileType = jsonData.getString("filetype");
         strFileData = jsonData.getString("filedata");
         btFile = decodeBASE64(strFileData);
         
         strFileName = CommonUtils.formatDateTime() + CommonUtils.generateSeq() + "." + strFileType;
         strFileUrl = CUSTOM_IMG_URL_PREFIX + "/" + strFileName;
         strFileName = CUSTOM_IMG_FILE_PATH + File.separator + strFileName;
         CommonUtils.saveFileData(strFileName, btFile);
         
         customData = listQuery.get(0);
         customData.setCustAccount(jsonData.getString("user"));
         customData.setCustImgfile(strFileName);
         customData.setCustImgurl(strFileUrl);
         
         if(jsonData.getString("name") != null && jsonData.getString("name").length() > 0)
            customData.setCustName(jsonData.getString("name"));
         if(jsonData.getString("city") != null && jsonData.getString("city").length() > 0)
            customData.setCustCity(jsonData.getString("city"));
         if(jsonData.getString("birthday") != null && jsonData.getString("birthday").length() > 0)
            customData.setCustBirthday(jsonData.getString("birthday"));

         //customData.setCustPasswd(jsonData.getString("pass"));
         //customData.setCustMobile(jsonData.getString("mobile"));
         //customData.setCustEmail(jsonData.getString("email"));
         //customData.setCustSex(jsonData.getString("sex")!=null ? new Integer(jsonData.getString("sex")) : 0);
         //customData.setCustVocation(jsonData.getString("vocation"));
         
         update(customData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eRegist)
      {
         nStatus = 10000;
         eRegist.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "保存用户资料成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else
            mapResult.put("message", "保存用户资料失败！");
      }
      
      return mapResult;
   }
   
   public Map customDetail(JSONObject jsonData)
   {
      int nStatus = 0;
      String CustAccount = "";
      List listQuery = null;
      Custom customData = null;
      JSONObject jsonCustom = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
            
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         customData = (Custom) listQuery.get(0);
         jsonCustom = new JSONObject();
         jsonCustom.put("user", customData.getCustAccount());
         jsonCustom.put("name", customData.getCustName());
         jsonCustom.put("sex", customData.getCustSex());
         jsonCustom.put("biarthday", customData.getCustBirthday());
         jsonCustom.put("city", customData.getCustCity());
         jsonCustom.put("vocation", customData.getCustVocation());
         jsonCustom.put("mobile", customData.getCustMobile());
         jsonCustom.put("email", customData.getCustEmail());
         jsonCustom.put("customImg", customData.getCustImgurl());
         jsonCustom.put("totalstar", customData.getTotalStar());
         jsonCustom.put("praiseCount", customData.getPraiseCount());
         jsonCustom.put("registdate", customData.getCreateDateString());
         jsonCustom.put("lastlogindate", customData.getLastLogindateString());
         
         jsonCustom.put("wishcount", customData.getWishCount());
         jsonCustom.put("otherwishcount", customData.getOtherwishCount());
         jsonCustom.put("attentioncount", customData.getAttentionCount());
         
         mapResult.put("custominfo", jsonCustom);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   public Map customUpdate(JSONObject jsonData)
   {
      int nStatus = 0;
      Custom customData = null;
      List<Custom> listQuery = null;
      String CustAccount = "", CustPasswd = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
         CustPasswd = jsonData.getString("pass");
         if(CustPasswd == null)   CustPasswd = "";
      
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            //客户不存在
            throw new QfdException(2000);
         
         customData = listQuery.get(0);
         if(jsonData.getString("newPass") != null && jsonData.getString("newPass").length() > 0 &&
            CustPasswd != null || CustPasswd.length() > 0)
         {
            //验证密码
            if(CustPasswd.equals(customData.getCustPasswd()) == false)
               //密码错误
               throw new QfdException(6001);
            customData.setCustPasswd(jsonData.getString("newPass"));
         }
         
         customData.setCustName(jsonData.getString("name"));
         customData.setCustCity(jsonData.getString("city"));
         customData.setCustBirthday(jsonData.getString("birthday"));
         
         if(jsonData.getString("mobile") != null && jsonData.getString("mobile").length() > 0)
            customData.setCustMobile(jsonData.getString("mobile"));
         if(jsonData.getString("email") != null && jsonData.getString("email").length() > 0)
            customData.setCustEmail(jsonData.getString("email"));
         if(jsonData.getString("vocation") != null && jsonData.getString("vocation").length() > 0)
            customData.setCustVocation(jsonData.getString("vocation"));
         if(jsonData.getString("sex") != null && jsonData.getString("sex").length() > 0)
            customData.setCustSex(jsonData.getString("sex")!=null ? new Integer(jsonData.getString("sex")) : 0);

         update(customData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eRegist)
      {
         nStatus = 10000;
         eRegist.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "修改成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else if(nStatus == 6001)
            mapResult.put("message", "原密码不正确！");
         else
            mapResult.put("message", "修改失败！");
      }
      
      return mapResult;
   }
   
   public Map customLogin(JSONObject jsonData)
   {
      int nStatus = 0;
      Stardeal starData = null;
      Custom customData = null;
      Version versionData = null;
      List<Custom> listQuery = null;
      List<Version> listVersion = null;
      List<Stardeal> listStardeal = null;
      java.sql.Timestamp lastLoginTime = null;
      String CustAccount = "", CustPasswd = "";
      //java.sql.Timestamp nowStamp = null, lastStamp = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         mapQuery.put("systemType", jsonData.getString("systemType"));
         //mapQuery.put("terminalType", jsonData.getString("terminalType"));
         listVersion = versionManager.findList(mapQuery);
         if(listVersion.size() > 0)
            versionData = listVersion.get(0);
         
         mapResult.put("latestVersion", (versionData==null) ? "" : versionData.getVersionNumber());
         mapResult.put("versionUrl", (versionData==null) ? "" : versionData.getVersionUrl());
         mapResult.put("versionDesc", (versionData==null) ? "" : versionData.getVersionDesc());
         mapResult.put("baseImg", (versionData==null) ? "" : versionData.getBaseimgUrl());
         mapResult.put("fileSize", (versionData==null) ? "" : versionData.getFileSize());
         //mapResult.put("permission", (versionData==null) ? "" : versionData.getVersionDesc());
         listVersion.clear();
         listVersion = null;
         
         CustAccount = jsonData.getString("user");
         CustPasswd =  jsonData.getString("pass");
         if(CustPasswd == null)   CustPasswd = "";
      
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         //验证密码
         customData = listQuery.get(0);
         if(CustPasswd.equals(customData.getCustPasswd()) == false)
            throw new QfdException(2003);
         
         //客户头像
         mapResult.put("customImg", customData.getCustImgurl());
         
         //首次登录赠送star，每日登录赠送star
         if(customData.getLastLogindate() == null)
         {
            starData = new Stardeal();
            starData.setCustId(customData.getCustId());
            starData.setReceiverId(customData.getCustId());
            starData.setOrderType(Stardeal.DEAL_TYPE_FIRSTLOGIN);
            starData.setOrderMoney(0);
            starData.setOrderCount(Stardeal.CUSTOM_FIRSTLOGIN_PRESENT_STAR);
            starData.setOrderStatus(1);
            stardealManager.save(starData);
            
            customData.setTotalStar(20);
         }
         else
         {
//            nowStamp = customData.getLastLogindate();
//            lastStamp = new java.sql.Timestamp(System.currentTimeMillis());
//            if(lastStamp.getYear() != nowStamp.getYear() ||
//               lastStamp.getMonth() != nowStamp.getMonth() ||
//               lastStamp.getDay() != nowStamp.getDay())
//            {
//               starData = new Stardeal();
//               starData.setCustId(customData.getCustId());
//               starData.setReceierId(customData.getCustId());
//               starData.setOrderType(Stardeal.DEAL_TYPE_EVERYLOGIN);
//               starData.setOrderMoney(0);
//               starData.setOrderCount(Stardeal.CUSTOM_EVERYLOGIN_PRESENT_STAR);
//               starData.setOrderStatus(1);
//               stardealManager.save(starData);
//            }
            
            //每天首次放灯赠送star
            mapQuery.clear();
            mapQuery.put("custId", customData.getCustId());
            mapQuery.put("orderType", Stardeal.DEAL_TYPE_EVERYLOGIN);
            mapQuery.put("todayHappen", 1);
            listStardeal = stardealManager.findList(mapQuery);
            
            //每天的首次登录
            if(listStardeal != null && listStardeal.size() == 0)
            {
               starData = new Stardeal();
               starData.setCustId(customData.getCustId());
               starData.setReceiverId(customData.getCustId());
               starData.setOrderType(Stardeal.DEAL_TYPE_EVERYLOGIN);
               starData.setOrderMoney(0);
               starData.setOrderCount(Stardeal.CUSTOM_EVERYLOGIN_PRESENT_STAR);
               starData.setOrderStatus(1);
               stardealManager.save(starData);
               
               customData.setTotalStar(customData.getTotalStar() + Stardeal.CUSTOM_USELIGHT_PRESENT_STAR);
               //customManager.update(customData);
               
               mapResult.put("presentstar", Stardeal.CUSTOM_EVERYLOGIN_PRESENT_STAR);
            }
            else
            {
               mapResult.put("presentstar", "0");
            }
         }
         
         //更新最后登录时间、次数
         lastLoginTime = new java.sql.Timestamp(System.currentTimeMillis());
         customData.setLastLogindate(lastLoginTime);
         customData.setLoginCount(customData.getLoginCount()+1);
         
         //更新用户终端信息
//         customData.setLastLogindate(jsonData.getString("systemVersion"));
//         customData.setLastLogindate(jsonData.getString("version"));
//         customData.setLastLogindate(jsonData.getString("terminalType"));
//         customData.setLastLogindate(jsonData.getString("imei"));
         
         update(customData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "登录成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "登录失败！");
      }
      
      return mapResult;
   }
   
   public Map customResetPasswd(JSONObject jsonData)
   {
      int nStatus = 0;
      Custom customData = null;
      Random randNumber = null;
      SendTextMail SendEmail = null;
      List<Custom> listQuery = null;
      String CustAccount = "", CustPasswd = "", strContent = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         randNumber = new Random(1000);
         CustPasswd = Integer.toString(randNumber.nextInt(99999999));
         
         customData = listQuery.get(0);
         customData.setCustPasswd(CustPasswd);
         update(customData);
         
         //发送邮件
         strContent = "你的密码已经被重置为：" + CustPasswd;
         SendEmail = new SendTextMail();
         SendEmail.setMailTo(CustAccount);
         SendEmail.setMailSubject("重置祈福灯登录密码");
         SendEmail.setMailBody(strContent);
         SendEmail.sendMail();
         
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eRegist)
      {
         nStatus = 10000;
         eRegist.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "重置密码成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "重置密码失败！");
      }
      
      return mapResult;
   }
   
   
   
   public Map customForecast(JSONObject jsonData)
   {
      String strContent = "", strBirthday = "";
      int nStatus = 0, nRegDay = 0, nCurveType = 0, nDayValue = 0, nTextLevel = 0;
      Qfdtext qfdtextData = null;
      Custom customData = null;
      List listQuery = null;
      Curve CurveData = null;
      Constellation constellationData = null;
      String CustAccount = "", CustPasswd = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         mapQuery.clear();
         CustAccount = (String) jsonData.get("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
         {
            nStatus = 2000;
         }
         else
         {
            customData = (Custom) listQuery.get(0);
            strBirthday = customData.getCustBirthday();
            nRegDay = customData.getRegistDaycount();
            nCurveType = customData.getCurveType();
            //System.out.println("nRegDay[" + nRegDay + "] nCurveType[" + nCurveType + "]");
            
            //System.out.println(strBirthday.substring(4));
            //星座
            mapQuery.clear();
            mapQuery.put("queryDate", strBirthday.substring(4));
            listQuery = constellationManager.findList(mapQuery);
            constellationData = (Constellation)listQuery.get(0);
            
            mapQuery.clear();
            mapQuery.put("curveType", nCurveType);
            mapQuery.put("dataDay", nRegDay);
            listQuery = curveManager.findList(mapQuery);
            CurveData = (Curve)listQuery.get(0);
            nDayValue = CurveData.getDataValue();
            
            //区别：上中下
            if(nDayValue <= 15) nTextLevel = 3;
            else if(nDayValue <= 35) nTextLevel = 2;
            else nTextLevel = 1;
            
            //事业
            mapQuery.clear();
            mapQuery.put("textType", 1);
            mapQuery.put("textSubtype", 2);
            mapQuery.put("textLevel", nTextLevel);
            mapQuery.put("selectDay", nRegDay);
            System.out.println("customForecast - nTextLevel[" + nTextLevel + "] nRegDay[" + nRegDay + "]");
            qfdtextData = (Qfdtext) qfdtextManager.findByDayLevel(mapQuery).get(0);
            strContent = strContent + qfdtextData.getTextContent();
            
            //爱情
            mapQuery.clear();
            mapQuery.put("textType", 1);
            mapQuery.put("textSubtype", 1);
            mapQuery.put("textLevel", nTextLevel);
            mapQuery.put("selectDay", nRegDay);
            qfdtextData = (Qfdtext) qfdtextManager.findByDayLevel(mapQuery).get(0);
            strContent = strContent + qfdtextData.getTextContent();
            
            nStatus = 1000;
            mapResult.put("fortune", strContent);
            mapResult.put("constellation", constellationData.getConstellationName());
            mapResult.put("luckvalue", nDayValue);
            //qfdtextManager.findList(mapQuery);
         }
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   public Map customPray(JSONObject jsonData)
   {
      int nStatus = 0, nDim = 0;
      Random randNumber = null;
      Qfdtext qfdtextData = null;
      Stardeal starData = null;
      Staruse staruseData = null;
      Custom customData = null;
      Version versionData = null;
      JSONObject jsonResult = null;
      List listQuery = null;
      List<Version> listVersion = null;
      String CustAccount = "", CustPasswd = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = (String) jsonData.get("user");
         
         //用户是否存在
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         
         //检测star是否足够
         customData = (Custom)listQuery.get(0);
         if(customData.getTotalStar() < CUSTOM_PRAYLUCK_STAR)
            throw new QfdException(8001);

         //检测用户当天是否已经开运
         
         randNumber = new Random();
         
         //方位
         mapQuery.clear();
         mapQuery.put("textType", 11);
         listQuery = qfdtextManager.findList(mapQuery);
         nDim = java.lang.Math.abs(randNumber.nextInt());
         nDim = nDim%listQuery.size();
         qfdtextData = (Qfdtext)listQuery.get(nDim);
         mapResult.put("luckpos", qfdtextData.getTextContent());
         
         //颜色
         mapQuery.clear();
         mapQuery.put("textType", 12);
         listQuery = qfdtextManager.findList(mapQuery);
         nDim = java.lang.Math.abs(randNumber.nextInt());
         nDim = nDim%listQuery.size();
         qfdtextData = (Qfdtext)listQuery.get(nDim);
         mapResult.put("luckcolor", qfdtextData.getTextContent());
         
         //物品
         mapQuery.clear();
         mapQuery.put("textType", 13);
         listQuery = qfdtextManager.findList(mapQuery);
         nDim = java.lang.Math.abs(randNumber.nextInt());
         nDim = nDim%listQuery.size();
         qfdtextData = (Qfdtext)listQuery.get(nDim);
         mapResult.put("luckarticle", qfdtextData.getTextContent());
         
         //扣除star数量，保存star使用记录
         staruseData = new Staruse();
         staruseData.setCustId(customData.getCustId());
         staruseData.setReceiverId(customData.getCustId());
         staruseData.setUseType(staruseData.USE_TYPE_PRAY);
         staruseData.setUseCount(CUSTOM_PRAYLUCK_STAR);
         staruseManager.save(staruseData);
         
         customData.setTotalStar(customData.getTotalStar() - CUSTOM_PRAYLUCK_STAR);
         customDao.update(customData);
         
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "开运成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2001)
            mapResult.put("message", "用户已存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else if(nStatus == 8001)
            mapResult.put("message", "幸运星数量不足！");
         else
            mapResult.put("message", "开运失败！");
      }
      
      return mapResult;
   }

   public Map cutomPraise(JSONObject jsonData)
   {
      int nStatus = 0;
      Integer CustqfdID;
      String CustAccount = "";
      Timestamp stopTime = null;
      Staruse staruseData = null;
      Custqfd custqfdData = null;
      Custom customData = null, objData = null;
      List listQuery = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
         //CustqfdID = jsonData.getInt("custqfdid");
         
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         customData = (Custom) listQuery.get(0);

         objData = (Custom) customDao.getById(jsonData.getInt("custid"));
         if(objData == null)
            throw new QfdException(2000);
         
         objData.setPraiseCount(objData.getPraiseCount()+1);
         customDao.update(objData);
         
//         mapQuery.clear();
//         listQuery.clear();
//         mapQuery.put("custqfdId", CustqfdID);
//         listQuery = custqfdDao.findList(mapQuery);
//         if(listQuery.size() == 0 || CustqfdID == null)
//            throw new QfdException(5000);
//         
//         custqfdData = (Custqfd) listQuery.get(0);
//         custqfdData.setPraiseCount(custqfdData.getPraiseCount()+1);
//         update(custqfdData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 5000)
            mapResult.put("message", "祈福灯不存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else if(nStatus == 8001)
            mapResult.put("message", "幸运星数量不足！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   public Map cutomShareBonus(JSONObject jsonData)
   {
      int nStatus = 0;
      String CustAccount = "";
      Stardeal starData = null;
      Custom customData = null;
      List<Custom> listQuery = null;
      List<Stardeal> listStardeal = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
         
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customDao.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         customData = listQuery.get(0);
         
         //每天首次分享赠送star
         mapQuery.clear();
         mapQuery.put("custId", customData.getCustId());
         mapQuery.put("orderType", Stardeal.DEAL_TYPE_SHAREBONUS);
         mapQuery.put("todayHappen", 1);
         listStardeal = stardealManager.findList(mapQuery);
         
         if(listStardeal != null && listStardeal.size() == 0)
         {
            starData = new Stardeal();
            starData.setCustId(customData.getCustId());
            starData.setReceiverId(customData.getCustId());
            starData.setOrderType(Stardeal.DEAL_TYPE_SHAREBONUS);
            starData.setOrderMoney(0);
            starData.setOrderCount(Stardeal.CUSTOM_SHAREBONUS_PRESENT_STAR);
            starData.setOrderStatus(1);
            stardealManager.save(starData);
            
            customData.setTotalStar(customData.getTotalStar() + Stardeal.CUSTOM_SHAREBONUS_PRESENT_STAR);
            customDao.update(customData);
            
            mapResult.put("presentstar", Stardeal.CUSTOM_SHAREBONUS_PRESENT_STAR);
         }
         else
         {
            mapResult.put("presentstar", "0");
         }
         
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eShare)
      {
         nStatus = 10000;
         eShare.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 5000)
            mapResult.put("message", "祈福灯不存在！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }

   private String encodeBASE64(byte[] btData)
   {
      String strCode = "";
      
      strCode = new sun.misc.BASE64Encoder().encode(btData);
      return strCode;
   } 

   private byte[] decodeBASE64(String strData)
   {
      byte[] btData = null;
      BASE64Decoder decoder = new BASE64Decoder();
      
      try
      {
         btData = decoder.decodeBuffer(strData);
      }
      catch(Exception eDecode)
      {
         btData = null;
         eDecode.printStackTrace();
      }
      
      return btData;
   }



}
